package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.*;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import com.google.gson.Gson;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class SaleListGoodsServiceImpl implements SaleListGoodsService {

    @Autowired
    private SaleListGoodsDao saleListGoodsDao;

    @Autowired
    private SaleListDao saleListDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private CustomerDao customerDao;

    @Autowired
    private PurchaseListDao purchaseListDao;

    @Override
    public ServiceVO save(SaleList saleList, String saleListGoodsStr, String saleNumber) {
        SaleList saleListVo = new SaleList();
        saleListVo.setSaleNumber(saleNumber);
        saleListVo.setAmountPaid(saleList.getAmountPaid());
        saleListVo.setAmountPayable(saleList.getAmountPayable());
        saleListVo.setSaleDate(saleList.getSaleDate());
        saleListVo.setRemarks(saleList.getRemarks());
        saleListVo.setState(saleList.getState());
        saleListVo.setCustomerId(saleList.getCustomerId());
        saleListVo.setUserId(1);// TODO 直接给了，不知道怎么获取

        Customer customer= customerDao.getCustomerById(saleList.getCustomerId());
        saleListVo.setCustomerName(customer.getCustomerName());

        User userById = userDao.getUserById(saleListVo.getUserId());//TODO
        saleListVo.setTrueName(userById.getTrueName());
        saleListDao.save(saleListVo);

        Gson gson = new Gson();
        SaleListGoods[] saleListGoods = gson.fromJson(saleListGoodsStr, SaleListGoods[].class);

        for (SaleListGoods saleListGood : saleListGoods) {
            SaleListGoods saleListGoodVo = new SaleListGoods();
            saleListGoodVo.setGoodsId(saleListGood.getGoodsId());
            saleListGoodVo.setGoodsCode(saleListGood.getGoodsCode());
            saleListGoodVo.setGoodsName(saleListGood.getGoodsName());
            saleListGoodVo.setGoodsModel(saleListGood.getGoodsModel());
            saleListGoodVo.setGoodsUnit(saleListGood.getGoodsUnit());
            saleListGoodVo.setGoodsNum(saleListGood.getGoodsNum());
            saleListGoodVo.setPrice(saleListGood.getPrice());
            saleListGoodVo.setTotal(saleListGood.getTotal());
            saleListGoodVo.setSaleListId(saleListVo.getSaleListId());
            saleListGoodVo.setGoodsTypeId(saleListGood.getGoodsTypeId());
            saleListGoodsDao.save(saleListGoodVo);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<SaleList> list(String saleNumber, Integer customerId, Integer state, String sTime, String eTime) {

        List<SaleList> lists = saleListDao.getList(saleNumber, customerId, state, sTime, eTime);
        for (SaleList list : lists) {
            Integer customerIdVo = list.getCustomerId();
            Customer customerById = customerDao.getCustomerById(customerIdVo);
            list.setCustomerName(customerById.getCustomerName());
            Integer userId = list.getUserId();
            User userById = userDao.getUserById(userId);
            list.setTrueName(userById.getTrueName());
        }
        return lists;
    }

    @Override
    public List<SaleListGoods> goodsList(Integer saleListId) {
        return saleListGoodsDao.goodsList(saleListId);
    }

    @Override
    public ServiceVO delete(Integer saleListId) {

        //先判断有没有详情
        List<SaleListGoods> goodsList = saleListGoodsDao.goodsList(saleListId);
        if (goodsList!=null){//详情有数据，循环删除
            for (SaleListGoods saleListGoods : goodsList) {
                Integer saleListGoodsId = saleListGoods.getSaleListGoodsId();
                saleListGoodsDao.deleteById(saleListGoodsId);
            }
        }
        //走到这，该purchaseListId在详情表一定是没有数据的
        saleListDao.deleteById(saleListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO updateState(Integer saleListId) {
        saleListDao.updateState(saleListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public List<Map<String, Object>> getSaleDataByDay(String sTime, String eTime) {
        //日销售总额、成本总额、盈利总额数据列表
        //date saleTotal purchasingTotal profit
        List<Map<String, Object>> mapLists = new LinkedList<>();
        List<SaleList> lists1=saleListDao.getSaleDataByDay(sTime,eTime);//返回date
        List<PurchaseList> lists2=purchaseListDao.getSaleDataByDay(sTime,eTime);//返回date

        getdate(mapLists, lists1, lists2);
        return mapLists;
    }

    private void getdate(List<Map<String, Object>> mapLists, List<SaleList> lists1, List<PurchaseList> lists2) {
        if (lists1 != null){
            for (SaleList saleList : lists1) {
                Map<String, Object> map = new HashMap<>();
                String saleDate = saleList.getSaleDate();
                map.put("date",saleDate);
                map.put("saleTotal",saleList.getAmountPaid());
                map.put("purchasingTotal",0);
                mapLists.add(map);
            }
        }
        if (lists2!=null){
            List<Map<String, Object>> maps = new LinkedList<>();
            for (Map<String, Object> mapList : mapLists) {
                Boolean flag =false;
                for (PurchaseList purchaseList : lists2) {
                    flag=mapList.get("date")==purchaseList.getPurchaseDate();
                    if (flag){
                        continue;
                    }
                }
                if (flag){//是同一天
                    for (PurchaseList purchaseList : lists2) {
                        mapList.replace("purchasingTotal",purchaseList.getAmountPaid());
                    }
                }else {//不是同一天
                    for (PurchaseList purchaseList : lists2) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("date",purchaseList.getPurchaseDate());
                        map.put("saleTotal",0);
                        map.put("purchasingTotal",purchaseList.getAmountPaid());
                        maps.add(map);
                    }
                }
            }
            for (Map<String, Object> map : maps) {
                mapLists.add(map);
            }
        }
        for (Map<String, Object> mapList : mapLists) {
            Object saleTotal = mapList.get("saleTotal");
            Object purchasingTotal = mapList.get("purchasingTotal");
            mapList.put("profit",Double.parseDouble(saleTotal.toString())-Double.parseDouble(purchasingTotal.toString()));
        }
    }

    @SneakyThrows
    @Override
    public List<Map<String, Object>> getSaleDataByMonth(String sTime, String eTime) {

        List<SaleList> lists1 = saleListDao.getSaleDataByMonth(sTime, eTime);
        List<PurchaseList> lists2 = purchaseListDao.getSaleDataByMonth(sTime, eTime);
        List<Map<String, Object>> mapLists = new LinkedList<>();
        if (lists1 != null){
            for (SaleList saleList : lists1) {
                Map<String, Object> map = new HashMap<>();
                map.put("date",saleList.getSaleDate());
                map.put("saleTotal",saleList.getAmountPaid());
                map.put("purchasingTotal",0);
                mapLists.add(map);
            }
        }
        if (lists2!=null){
            List<Map<String, Object>> maps = new LinkedList<>();
            for (Map<String, Object> mapList : mapLists) {
                Boolean flag =false;
                for (PurchaseList purchaseList : lists2) {
                    flag=mapList.get("date").toString()==purchaseList.getPurchaseDate();
                    if (flag){
                        continue;
                    }
                }
                if (flag){//是同一个月
                    for (PurchaseList purchaseList : lists2) {
                        mapList.replace("purchasingTotal",purchaseList.getAmountPaid());
                    }
                }else {//不是同一个月
                    for (PurchaseList purchaseList : lists2) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("date",purchaseList.getPurchaseDate());
                        map.put("saleTotal",0);
                        map.put("purchasingTotal",purchaseList.getAmountPaid());
                        maps.add(map);
                    }
                }
            }
            for (Map<String, Object> map : maps) {
                mapLists.add(map);
            }
        }
        for (Map<String, Object> mapList : mapLists) {
            Object saleTotal = mapList.get("saleTotal");
            Object purchasingTotal = mapList.get("purchasingTotal");
            mapList.put("profit",Double.parseDouble(saleTotal.toString())-Double.parseDouble(purchasingTotal.toString()));
        }
        return mapLists;
    }
}
